{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook gives a simple example of how to use the ShaderMaterial to write custom shaders from the Python side. For further information about the shaders, consult the three.js docs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pythreejs import *\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ShaderMaterial?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vertex_shader = \"\"\"\n",
    "uniform float time;\n",
    "uniform vec2 resolution;\n",
    "\n",
    "void main() {\n",
    "    vec3 pos = vec3(position.x + time * resolution.x, position.y + time * resolution.y, position.z);\n",
    "    gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fragment_shader = \"\"\"\n",
    "uniform vec3 user_color;\n",
    "\n",
    "void main() {\n",
    "    #ifdef FIX_COLOR\n",
    "    gl_FragColor = vec4(0.5, 0, 0, 1.0);\n",
    "    #else\n",
    "    gl_FragColor = vec4(user_color, 1.0);\n",
    "    #endif\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "material = ShaderMaterial(\n",
    "    uniforms=dict(\n",
    "        time=dict(value=0.0),\n",
    "        resolution=dict(value=(1, 1)),\n",
    "        user_color=dict(value='green'),\n",
    "        **UniformsLib['common']\n",
    "    ),\n",
    "    defines=dict(\n",
    "        FIX_COLOR=1,\n",
    "    ),\n",
    "    vertexShader=vertex_shader,\n",
    "    fragmentShader=fragment_shader,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "material"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Disable fixed color from defines:\n",
    "material.defines = dict()\n",
    "material.needsUpdate = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Uniform editors:\n",
    "picker = widgets.ColorPicker(value=material.uniforms['user_color']['value'])\n",
    "time_slider = widgets.FloatSlider(value=material.uniforms['time']['value'], min=-15, max=15)\n",
    "\n",
    "# Interactive code:\n",
    "def update_uniforms(change):\n",
    "    uniforms = dict(**material.uniforms)\n",
    "    uniforms.update(\n",
    "        time=dict(value=time_slider.value),\n",
    "        user_color=dict(value=picker.value),\n",
    "    )\n",
    "    material.uniforms = uniforms\n",
    "    material.needsUpdate = True\n",
    "\n",
    "picker.observe(update_uniforms)\n",
    "time_slider.observe(update_uniforms)\n",
    "\n",
    "# Present \"dashboard\" in VBox:\n",
    "widgets.VBox([\n",
    "    Preview(material),\n",
    "    picker,\n",
    "    time_slider,\n",
    "])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
